package ua.kpi.flats.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import ua.kpi.flats.service.UserService;


/**
 * 
 * @author vanish_com
 * Custom AuthenticationProvider for Spring Security 
 */
public class CustomUserDetailsService implements UserDetailsService {
		
	private UserService userService;
	
	/**
	 * This method try load user data from database by his 
	 * username. If user presents in db - return User instance,
	 * else throw exception
	 * @param username - Login which user inserted	 * 
	 */
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		return userService.getUser(username);		
	}
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
